/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controlador;

import Facade.LlamadoFacade;
import Facade.MaquinariaFacade;
import Facade.TelefonistaFacade;
import Facade.VoluntarioFacade;
import VO.LlamadoVO;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import vista.LlamadoBean;

/**
 *
 * @author Jorge
 */
public class LlamadoAction extends DispatchAction {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String LOAD = "load";
    private final static String ERROR ="error";
    private final static String VOLVER ="volver";
    
    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward Registrar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String vols[],maqs[];
        Vector lista,listaVol,listaMaq;
        Vector volsVector = new Vector(),maqsVector=new Vector();
        LlamadoVO llamado = new LlamadoVO();
        LlamadoFacade fcd = new LlamadoFacade();
       TelefonistaFacade fcdT =  new TelefonistaFacade();
        VoluntarioFacade volFcd = new VoluntarioFacade();
        MaquinariaFacade maqFcd = new MaquinariaFacade();




        LlamadoBean datos= (LlamadoBean)form;
        
        vols = request.getParameterValues("vol");
        maqs = request.getParameterValues("maq");
        
        vols= vols[0].split(",");
        maqs = maqs[0].split(",");

        for(int i=1;i<vols.length;i++)
          volsVector.add(vols[i]);

        for(int i=1;i<maqs.length;i++)
            maqsVector.add(maqs[i]);


        datos.setMaquinarias(maqsVector);
        datos.setVoluntarios(volsVector);

        //validando
        if(datos.getFecha().isEmpty()||datos.getFonoSolicitante().isEmpty()|| datos.getNombreSolicitante().isEmpty()
           || datos.getMaquinarias().isEmpty() || datos.getVoluntarios().isEmpty())
        {
            datos.setAviso("<span style='color:red;font-weight:bold'>Debe ingresar todos los campos</span>");
            lista = fcdT.cargarIdTelefonistas();
        listaVol = volFcd.cargarVoluntarios();
        listaMaq = maqFcd.cargarNomenclaturas();

        request.setAttribute("rutVoluntarios", listaVol);
        request.setAttribute("listaTelefonistas", lista);
        request.setAttribute("listMaquinarias", listaMaq);
            request.setAttribute("bean", datos);
            return mapping.findForward(ERROR);
        }

        llamado.setFecha(datos.getFecha());
        llamado.setFonoSolicitante(datos.getFonoSolicitante());
        llamado.setIdTelefonista(datos.getIdTelefonista());
        llamado.setMaquinarias(datos.getMaquinarias());
        llamado.setNombreSolicitante(datos.getNombreSolicitante());
        llamado.setTipo(datos.getTipo());
        llamado.setVoluntarios(datos.getVoluntarios());

        if(fcd.ingresarLlamado(llamado))
        {
             datos.setAviso("<span style='color:green;font-weight:bold'>Registro  Exitoso</span>");
             request.setAttribute("bean", datos);
            return mapping.findForward(SUCCESS);
        }
        else
            return mapping.findForward(ERROR);
    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward cargar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Vector lista,listaVol,listaMaq;
        TelefonistaFacade fcd =  new TelefonistaFacade();
        VoluntarioFacade volFcd = new VoluntarioFacade();
        MaquinariaFacade maqFcd = new MaquinariaFacade();
        lista = fcd.cargarIdTelefonistas();
        listaVol = volFcd.cargarVoluntarios();
        listaMaq = maqFcd.cargarNomenclaturas();

        request.setAttribute("rutVoluntarios", listaVol);
        request.setAttribute("listaTelefonistas", lista);
        request.setAttribute("listMaquinarias", listaMaq);

        
        return mapping.findForward(LOAD);
    }

    public ActionForward volver(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {



        return mapping.findForward(VOLVER);
    }
}